package com.ylr.unittest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class UserService {

    @Resource
    UserDao userDao;

    @Resource
    MoneyDAO moneyDAO;

    public UserVO getUserById(int uid){
        List<UserDTO> users = userDao.getUserInfo();
        UserDTO userDTO = users.stream().filter(u -> u.getId() == uid).findFirst().orElse(null);
        UserVO userVO = new UserVO();
        if(null == userDTO){
            return userVO;
        }
        userVO.setId(userDTO.getId());
        userVO.setName(userDTO.getName());
        userVO.setSex(userDTO.getSex());
        userVO.setAge(userDTO.getAge());
        // 显示邮编
        if("上海".equals(userDTO.getProvince())){
            userVO.setAbbreviation("沪");
            userVO.setPostCode(200000);
        }
        if("北京".equals(userDTO.getProvince())){
            userVO.setAbbreviation("京");
            userVO.setPostCode(100000);
        }
        // 手机号处理
        if(null != userDTO.getTelephone() && !"".equals(userDTO.getTelephone())){
            userVO.setTelephone(userDTO.getTelephone().substring(0,3)+"****"+userDTO.getTelephone().substring(7));
        }

        return userVO;
    }

    /**
     * 根据汇率计算金额
     * @param userVO
     */
    public void setOrderAmountByExchange(UserVO userVO){
        if(null == userVO.getUserOrders() || userVO.getUserOrders().size() <= 0){
            return ;
        }
        for(OrderVO orderVO : userVO.getUserOrders()){
            BigDecimal amount = orderVO.getAmount();
            // 获取汇率(调用汇率接口)
            BigDecimal exchange = moneyDAO.getExchangeByCountry(userVO.getCountry());
            amount = amount.multiply(exchange); // 根据汇率计算金额
            orderVO.setAmount(amount);
        }
    }

    /**
     * 校验请求参数user是否合法
     * @param user
     * @throws APIException
     */
    public void validateUser(UserVO user) throws APIException {
        if(user == null){
            throw new APIException("10001", "user is null");
        }
        if(null == user.getName() || "".equals(user.getName())){
            throw new APIException("10002", "user name is null");
        }
        if(user.getAge() == 0){
            throw new APIException("10003", "user age is null");
        }
        if(null == user.getTelephone() || "".equals(user.getTelephone())){
            throw new APIException("10004", "user telephone is null");
        }
        if(null == user.getSex() || "".equals(user.getSex())){
            throw new APIException("10005", "user sex is null");
        }
        if(null == user.getUserOrders() || user.getUserOrders().size() <= 0){
            throw new APIException("10006", "user order is null");
        }
        for(OrderVO order : user.getUserOrders()) {
            if (null == order.getOrderNum() || "".equals(order.getOrderNum())) {
                throw new APIException("10007", "order number is null");
            }
            if (null == order.getAmount()) {
                throw new APIException("10008", "order amount is null");
            }
        }
        throw new APIException("10009", "yyds");
    }




}